<?php

namespace application\model;

use application\entity\db\Catelog;
use application\entity\enu\CatelogType;
use application\model\BaseModel;
use core\entity\Criteria;
use core\entity\Query;
use core\entity\Sort;
use application\entity\enu\Boolean;

class CatelogModel extends BaseModel {

    public function __construct() {
        parent::__construct(Catelog::class);
    }

    /**
     * Danh mục theo kiểu danh mục
     * @param CatelogType $type
     * @param Boolean $active
     * @return Catelog[]
     */
    public function getByType($type = CatelogType::APP, $active = Boolean::NONE) {
        $cri = new Criteria();
        $cri->andKey("type")->is($type);
        if ($active > Boolean::NONE) {
            $cri->andKey("active")->is($active == Boolean::TRUE);
        }
        $sort = new Sort("positon", Sort::ASC);
        $query = new Query($cri);
        $query->withSort($sort);
        return $this->ifind($query);
    }

    /**
     * Danh mục theo danh mục cha
     * @param type $parentId
     * @param type $active
     * @return type
     */
    public function getByParentId($parentId, $active = Boolean::NONE) {
        $cri = new Criteria();
        $cri->andKey("parentId")->is($parentId);
        if ($active > Boolean::NONE) {
            $cri->andKey("active")->is($active == Boolean::TRUE);
        }
        $sort = new Sort("positon", Sort::ASC);
        $query = new Query($cri);
        $query->withSort($sort);
        return $this->ifind($query);
    }

    /**
     * Đếm danh mục con
     * @param type $parentId
     * @param type $active
     * @return type
     */
    public function countByParentId($parentId, $active = Boolean::NONE) {
        $cri = new Criteria();
        $cri->andKey("parentId")->is($parentId);
        if ($active > Boolean::NONE) {
            $cri->andKey("active")->is($active == Boolean::TRUE);
        }
        return $this->count($cri);
    }

    /**
     * Tìm theo seo url
     * @param type $seoUrl
     * @return type
     */
    public function getBySeoUrl($seoUrl) {
        $cri = new Criteria();
        $cri->andKey("seoUrl")->is($seoUrl);
        $sort = new Sort("positon", Sort::ASC);
        $query = new Query($cri);
        $query->withSort($sort);
        return $this->ifindOne($query);
    }

    /**
     * Kiểm tra seo url có tồn tại trên hệ thống
     * @param type $seoUrl
     * @return type
     */
    public function exitBySeoUrl($seoUrl) {
        $cri = new Criteria();
        $cri->andKey("seoUrl")->is($seoUrl);
        return $this->icount($cri) > 0;
    }

    /**
     * Change all theo danh mục cha
     * @param type $parentId
     * @param type $active
     */
    public function updateActiveByParentId($parentId, $active) {
        $cri = new Criteria();
        $cri->andKey("parentId")->is($parentId);
        $query = new Query($cri);
        $this->update($query, ["active" => ($active == Boolean::TRUE)]);
    }

}
